package net.uman.web.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uman.entity.Article;
import net.uman.entity.User;
import net.uman.framework.modules.orm.Page;
import net.uman.framework.modules.orm.PropertyFilter;
import net.uman.framework.modules.security.springsecurity.SpringSecurityUtils;
import net.uman.framework.modules.web.struts2.Struts2Utils;
import net.uman.service.ArticleManager;
import net.uman.util.Constants;
import net.uman.util.TextFormatter;
import net.uman.web.CrudActionSupport;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 新闻管理
 * @author jinlong-dt
 *
 */
@Namespace("/manager")
public class NewsAction extends CrudActionSupport<Article>{
	private static final long serialVersionUID = 716651143673960673L;
	@Autowired
	private ArticleManager articleManager;
	public Long id;
	public int articleType;
	public String articleTypeName;
	public  Article entity;
	public Page<Article> newsPage = new Page<Article>(10);
	public Article getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		if(articleType == Constants.ARTICLE_TYPE_NEWS){
			articleTypeName =Constants.ARTICLE_TYPE_NEWS_CAPTION;
		}else if(articleType == Constants.ARTICLE_TYPE_JOB){
			articleTypeName =Constants.ARTICLE_TYPE_JOB_CAPTION;
		}
		return "list";
	}

	public String newsListData() throws Exception{
		newsPage.setPageNo(pageNum);
		JSONObject jsonResult = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		getPageData(articleType);
		for(Article article : newsPage.getResult()){
			JSONObject jsonNews = new JSONObject();
			jsonNews.put("id",article.getId());
			jsonNews.put("keyWord", article.getKeyWord());
			jsonNews.put("authorName", article.getAuthorName());
			jsonNews.put("type", article.getType());
			jsonNews.put("addTime", TextFormatter.formatToSimpleDate(article.getAddDate()));
			jsonNews.put("title", article.getTitle());
			jsonArray.add(jsonNews);
		}
		jsonResult.put("total", newsPage.getTotalCount());
		jsonResult.put("rows", jsonArray);
		Struts2Utils.renderJson(jsonResult);
		return null;
	}
	
	private void getPageData(int type){
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter("filter_EQI_type",String.valueOf(type)));
		articleManager.getArticlePage(newsPage, filters);
	}
	
	@Override
	public String input() throws Exception {
		if(entity.getType() != null){
			articleType = entity.getType();
		}
		if(articleType == Constants.ARTICLE_TYPE_NEWS){
			articleTypeName =Constants.ARTICLE_TYPE_NEWS_CAPTION;
		}else if(articleType == Constants.ARTICLE_TYPE_JOB){
			articleTypeName =Constants.ARTICLE_TYPE_JOB_CAPTION;
		}
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		User user = SpringSecurityUtils.getCurrentUser();
		entity.setAddDate(new Date());
		entity.setAuthorId(user.getId());
		entity.setAuthorName(user.getName());
		entity.setStatus(Constants.STATUS_NORMAL);
		articleManager.insertNewsArticle(entity);
		articleType =entity.getType();
		resultUrl(entity.getType());
		return "list";
	}

	private void resultUrl(int type){
		if(type == Constants.ARTICLE_TYPE_NEWS){
			articleTypeName =Constants.ARTICLE_TYPE_NEWS_CAPTION;
		}else if(type == Constants.ARTICLE_TYPE_JOB){
			articleTypeName =Constants.ARTICLE_TYPE_JOB_CAPTION;
		}
	}
	
	@Override
	public String delete() throws Exception {
		if(id != null){
			articleManager.delete(id);
			Struts2Utils.renderText("删除成功");
		}else{
			Struts2Utils.renderText("删除失败");
		}
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		if(id != null){
			entity = articleManager.get(id);
		}else{
			entity = new Article();
		}
	}

}
